<?php

namespace App\Http\Controllers\Customer;

use App\Helpers\Code;
use App\Models\Customer\Customer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class CustomerController extends Controller
{

    /**
     * CustomerController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new Customer();

        $this->_validators();
    }

    /**
     * 添加客户
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addCustomer(Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['code', 'created_by', 'updated_by', 'data_scope']));

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addCustomer($data);
            $this->checked($model, [
                'null' => 'customer.type_not_exists',
                Code::INSIDE_ERROR => 'customer.region_province_not_rely',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑客户
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editCustomer(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'updated_by', 'data_scope', 'code']));

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'title':
                        $rules['title'] = ['bail', 'required', 'max:100', Rule::unique('customers')->ignore($id)];
                        break;
                    case 'address':
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editCustomer($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'customer.region_province_not_rely',
                Code::INSIDE_ERROR_1 => 'customer.type_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取客户及科室
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getCustomerTree(Request $request)
    {
        try {
            $keyword = $request->input('keyword') ?? null;

            $this->result['data'] = $this->model->getCustomerTree($keyword);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取客户详情
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function getCustomer($id)
    {
        try {
            $model = $this->model->getCustomer($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取客户列表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getCustomerList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getCustomerList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取部门列表
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function getCustomerDepartmentList($id)
    {
        try {
            $model = $this->model->getCustomerDepartmentList($id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'title' => 'bail|required|max:100|unique:customers',
            'description' => 'bail|nullable|max:200',
            'charged_by' => 'bail|nullable|exists:users,id',
            'type' => 'bail|required|max:200',
            'postcode' => 'bail|nullable|max:200',
            'email' => 'bail|nullable|max:100|email',
            'field' => 'bail|nullable|max:200',
            'phone' => 'bail|nullable|max:20',
            'region' => 'bail|required',
            'province' => 'bail|required',
            'state' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'title.required' => trans('customer.title_required'),
            'title.max' => trans('customer.title_max'),
            'title.unique' => trans('customer.title_unique'),
            'description.max' => trans('customer.description_max'),
            'charged_by.exists' => trans('customer.charged_by_exists'),
            'type.required' => trans('customer.type_required'),
            'type.max' => trans('customer.type_max'),
            'postcode.max' => trans('customer.postcode_max'),
            'email.max' => trans('customer.email_max'),
            'email.email' => trans('customer.email_email'),
            'field.max' => trans('customer.field_max'),
            'phone.max' => trans('customer.phone_max'),
            'region.required' => trans('customer.region_required'),
            'province.required' => trans('customer.province_required'),
            'state.required' => trans('customer.state_required'),
            'state.in' => trans('customer.state_in'),
        ];
    }

}
